package com.dreamchaser.familydevice.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by Administrator on 2018/9/13 16:44.
 * Discript :【DateTimeHelper的描述】
 */
public class DateTimeConvertUtil {


    static String defaultDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    static String defaultTimeFormat = "HH:mm:ss";
    static String defaultDateFormat = "yyyy-MM-dd";


    /**
     * 将date转成字符串
     *
     * @param date   Date
     * @param format SimpleDateFormat
     *               <br>
     *               注： SimpleDateFormat为空时，采用默认的yyyy-MM-dd HH:mm:ss格式
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStringByDate(Date date, String format) {
        DateTimeFormatter dateFormat = format == null ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") : DateTimeFormatter.ofPattern(format);
        LocalDateTime d = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return (date == null ? "" : d.format(dateFormat));
    }

    /**
     * 将字符串转成localdatetiime
     *
     * @param date   Date
     * @param format SimpleDateFormat
     *               <br>
     *               注： SimpleDateFormat为空时，采用默认的yyyy-MM-dd HH:mm:ss格式
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static LocalDateTime getLocalDateTimeByString(String date, String format) {
        DateTimeFormatter dateFormat = format == null ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") : DateTimeFormatter.ofPattern(format);
        LocalDateTime d = LocalDateTime.parse(date, dateFormat);
        return d;
    }

    /**
     * 将字符串转成localdate
     *
     * @param date   Date
     * @param format SimpleDateFormat
     *               <br>
     *               注： SimpleDateFormat为空时，采用默认的yyyy-MM-dd HH:mm:ss格式
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static LocalDate getLocalDateByString(String date, String format) {
        DateTimeFormatter dateFormat = format == null ? DateTimeFormatter.ofPattern("yyyy-MM-dd") : DateTimeFormatter.ofPattern(format);
        LocalDate d = LocalDate.parse(date, dateFormat);
        return d;
    }

    /**
     * 将date转成字符串
     *
     * @param localDate LocalDate
     * @param format    SimpleDateFormat
     *                  <br>
     *                  注： SimpleDateFormat为空时，采用默认的yyyy-MM-dd HH:mm:ss格式
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStringByLocalDateTime(LocalDateTime localDate, String format) {
        DateTimeFormatter dateFormat = format == null ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") : DateTimeFormatter.ofPattern(format);
        return (localDate == null ? "" : localDate.format(dateFormat));
    }

    /**
     * 将date转成字符串
     *
     * @param localDate LocalDate
     * @param format    SimpleDateFormat
     *                  <br>
     *                  注： SimpleDateFormat为空时，采用默认的yyyy-MM-dd HH:mm:ss格式
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStringByLocalDate(LocalDate localDate, String format) {
        DateTimeFormatter dateFormat = format == null ? DateTimeFormatter.ofPattern("yyyy-MM-dd") : DateTimeFormatter.ofPattern(format);
        return (localDate == null ? "" : localDate.format(dateFormat));
    }


    /**
     * 将"yyyy-MM-dd" 格式的字符串转成Date
     *
     * @param strDate
     * @return Date
     */
    public static Date getDateByString(String strDate) {
        LocalDateTime localDateTime = LocalDateTime.parse(strDate);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return date;
    }

    /**
     * localdatetime转换成date
     *
     * @param date
     * @return
     */
    public static Date getDateByLocalDateTime(LocalDateTime date) {
        return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date getDateByLocalDate(LocalDate date) {
        LocalDateTime localDateTime = LocalDateTime.of(date.getYear(), date.getMonth(), date.getDayOfMonth(), 0, 0);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * date转换成localdatetime
     *
     * @param date
     * @return
     */
    public static LocalDateTime getLocalDateTimeByDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 日期相差天数
     *
     * @return
     */
    public static int getDays(LocalDate start, LocalDate end) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateTimeConvertUtil.getDateByLocalDate(start));
        long m = calendar.getTimeInMillis();
        calendar.setTime(DateTimeConvertUtil.getDateByLocalDate(end));
        long m2 = calendar.getTimeInMillis();
        return Integer.valueOf(String.valueOf((m > m2 ? m - m2 : m2 - m) / (1000 * 3600 * 24)));
    }


}
